package com.ws.ssmzongjie3.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ws.ssmzongjie3.bean.Acl_User;
import com.ws.ssmzongjie3.bean.Carousel;
import com.ws.ssmzongjie3.bean.ProVo;
import com.ws.ssmzongjie3.bean.Route;
import com.ws.ssmzongjie3.service.CarouselService;
import com.ws.ssmzongjie3.util.Res;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import static net.sf.jsqlparser.util.validation.metadata.NamedObject.user;

@Controller
@RequestMapping("/lunbotu")
public class CarouselController {
    @Autowired
    private CarouselService carouselService;
    @RequestMapping("/list")
    public String select(Model model, ProVo proVo, Carousel carousel){
        System.out.println("links:"+carousel);

        PageHelper pageHelper = new PageHelper();//分页帮助类
        pageHelper.startPage(proVo.getCurrentPage(),proVo.getPageSize());//分页   每页条数
//        List<Carousel> carousels = carouselService.listCarousel(carousel);
        List<Carousel> carousels1 = carouselService.listCarouselWithRoute(carousel);
        PageInfo pageInfo = new PageInfo(carousels1);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("carousel",carousel);

        return "/WEB-INF/admin/carousel/list";
    }
    @RequestMapping("/add")
    public String add(Route route, Model model){
        model.addAttribute("route",route);
        return "/WEB-INF/admin/carousel/add";
    }
    @RequestMapping("/insert")
    public String insert(Carousel carousel, MultipartFile image) throws IOException {
        if ((image.getSize() != 0)||!(image.getOriginalFilename().equals(""))){
            System.out.println("文本框名字"+image.getName());
            System.out.println("文件名字"+image.getOriginalFilename());
            System.out.println("文件大小"+image.getSize());
            System.out.println("文件格式"+image.getContentType());

            String uuid = UUID.randomUUID().toString().replace("-", "");
            System.out.println(uuid);
            String fileName = uuid+"."+image.getOriginalFilename().split("\\.")[1];
            System.out.println(fileName);
            image.transferTo(new File(Res.fileDir+fileName));
            carousel.setPicture_address(fileName);
        }
        carouselService.insertCarousel(carousel);
        return "redirect:/lunbotu/list";
    }
    @RequestMapping("/delete")
    public String delete(Carousel carousel){
        Carousel carouselById = carouselService.getCarouselById(carousel);
        if (carouselById != null && carouselById.getPicture_address() != null) {
            File imageFile = new File(Res.fileDir + carouselById.getPicture_address());
            if (imageFile.exists()) {
                imageFile.delete();
            }
        }
        carouselService.deleteCarousel(carousel);
        return "redirect:/lunbotu/list";
    }
    @RequestMapping("/update")
    public String update(Carousel carousel, MultipartFile image) throws IOException {
        Carousel carouselById = carouselService.getCarouselById(carousel);
        if (carouselById != null && carouselById.getPicture_address() != null) {
            File imageFile = new File(Res.fileDir + carouselById.getPicture_address());
            if (imageFile.exists()) {
                imageFile.delete();
            }
        }

        if ((image.getSize() != 0)||!(image.getOriginalFilename().equals(""))){
            System.out.println("文本框名字"+image.getName());
            System.out.println("文件名字"+image.getOriginalFilename());
            System.out.println("文件大小"+image.getSize());
            System.out.println("文件格式"+image.getContentType());

            String uuid = UUID.randomUUID().toString().replace("-", "");
            System.out.println(uuid);
            String fileName = uuid+"."+image.getOriginalFilename().split("\\.")[1];
            System.out.println(fileName);
            image.transferTo(new File(Res.fileDir+fileName));
            carousel.setPicture_address(fileName);
        }
        carouselService.updateCarousel(carousel);
        return "redirect:/lunbotu/list";
    }

    @RequestMapping("/huixian")
    public String huixian(Carousel carousel, Model model) {
        Carousel carousel1 = carouselService.getCarouselById(carousel);
        model.addAttribute("carousel",carousel1);
        return "/WEB-INF/admin/carousel/update";
    }








}